﻿using System;
using System.Data;
using System.Data.DBLib;
using SysComm;

namespace SysOperationLayer.DBHandle
{
    public class ProjectClassDB : DBBase
    {
        public ProjectClassDB()
        {
        }

        public ProjectClassDB(CSqlDB objDB)
        {
            m_objDB = objDB;
        }

        public DataTable GetInfo()
        {
            if (ConnectDB())
            {
                string strSql = "select * from ProjectClass where IsDelete = 0";
                return m_objDB.getDataTable(strSql, "ProjectClass");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public DataTable getProjectClassList(string projectClass, string description, int isDelete)
        {
            if (ConnectDB())
            {
                string strSql = "select * from ProjectClass where 1=1";

                if (projectClass != "" && projectClass != null)
                {
                    strSql += " and ProjectClass like '%" + projectClass + "%'";
                }
                if (description != "" && description != null)
                {
                    strSql += " and Description like '%" + description + "%'";
                }

                strSql += " and IsDelete=" + isDelete;

                return m_objDB.getDataTable(strSql, "ProjectClass");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public DataRow GetItem(string strProjectClass)
        {
            if (ConnectDB())
            {
                string strSql = "select * from ProjectClass where ProjectClass='" + strProjectClass + "'";
                return m_objDB.getDataRow(strSql, "ProjectClass");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public int Insert(string projectClass, string description, string strUserID, string strUserIP)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "insert into ProjectClass(ProjectClass, Description";
                strSql += ", CreateIp, CreateBy, CreateDate, UpdateIp, UpdateBy, UpdateDate)";
                strSql += " values('" + projectClass + "','" + description + "','";
                strSql += strUserIP + "','" + strUserID + "',getDate(),'" + strUserIP + "','" + strUserID + "',getDate())";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Update(string projectClass, string description, string strUserID, string strUserIP)
        {
            if (IsEmpty(projectClass))
            {
                m_strMessage = "Project Class Code Is Empty!";
            }
            else if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update ProjectClass set ";
                strSql += " Description='" + description + "'";
                strSql += ", UpdateBy='" + strUserID + "'";
                strSql += ", UpdateIp='" + strUserIP + "'";
                strSql += ", UpdateDate=getDate()";
                strSql += " where ProjectClass='" + projectClass + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Delete(string projectClass, string strUserID, string strUserIP)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update ProjectClass set ";
                strSql += " isDelete=1";
                strSql += ", DeleteBy='" + strUserID + "'";
                strSql += ", DeleteIP='" + strUserIP + "'";
                strSql += ", DeleteDate=getDate()";
                strSql += " where ProjectClass='" + projectClass + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }


        public int Trash(string projectClass)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "delete from ProjectClass where ProjectClass='" + projectClass + "'";

                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Restore(string projectClass)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update ProjectClass set isDelete=0 where ProjectClass='" + projectClass + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }
    }
}
